<?php

namespace MoChat\App\Call\Action\Dashboard\Plan;

use MoChat\App\Call\Service\CallPlanService;
use MoChat\App\Sales\Contract\SalesTaskContract;
use MoChat\Framework\Action\AbstractAction;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\RequestMapping;
use MoChat\App\Common\Middleware\DashboardAuthMiddleware;
use MoChat\Framework\Constants\ErrorCode;
use MoChat\Framework\Exception\CommonException;
use MoChat\Framework\Request\ValidateSceneTrait;

/**
 * 呼叫计划编辑
 * @Controller
 */
class Update extends AbstractAction
{
    use ValidateSceneTrait;

    /**
     * @Inject
     * @var CallPlanService
     */
    protected $callPlanService;

    /**
     * @Middlewares({
     *     @Middleware(DashboardAuthMiddleware::class),
     * })
     * @RequestMapping(path="/dashboard/callPlan/update", methods="POST")
     */
    public function handle()
    {
        $user = user();
        // 判断用户绑定企业信息
        if (!isset($user['corpIds']) || count($user['corpIds']) !== 1) {
            throw new CommonException(ErrorCode::INVALID_PARAMS, '未选择登录企业，不可操作');
        }
        $data = $this->request->all();
        $this->validated($data);
        $this->callPlanService->updateContract($data['id'], [
            'name' => $data['name'],
            'plan_time' => $data['planTime'],
            'is_export_next_task' => $data['isExportNextTask'],
            'updated_at' => date('Y-m-d H:i:s'),
        ]);
        return [];
    }

    /**
     * 验证规则.
     */
    protected function rules(): array
    {
        return [
            'id' => 'required|integer',
            'name' => 'required|max:30',
            'planTime' => 'required',
            'isExportNextTask' => 'required|in:0,1',
        ];
    }

    /**
     * 获取已定义验证规则的错误消息.
     */
    protected function messages(): array
    {
        return [
            'id.required' => 'id必填',
            'id.integer' => 'id参数类型错误',
            'name.required' => '计划名称必填',
            'name.max' => '计划名称长度不能超过30个字符',
            'planTime.required' => '计划时间必填',
            'isExportNextTask.required' => '是否汇入下一阶段字段必填',
            'isExportNextTask.in' => '是否汇入下一阶段字段错误',
        ];
    }
}